<?php

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'兑换码列表'),
	array('op'=>'edit','title'=>'添加兑换码'),	
);
if($_RQ['op']=='list'){
	if($_RQ['type']){
		$_SESSION['extype']=$type=intval($_RQ['type']);
	}
	if($_RQ['id']){
		$_SESSION['exid']=intval($_RQ['id']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	if($_SESSION['exid']){
		$params['groupid']=$_SESSION['exid'];
		$return['params']['id']=$_SESSION['exid'];
	}
	if(!empty($_RQ['key'])){
		$params['key like']='%'.trim($_RQ['key']).'%';
		$return['params']['key']=$_RQ['key'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','endtime','key','status');
	$list=pdo_getslice('jy_exchange_code',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$v){
				$v['url']=custom_url('jy_exchange/index//code='.$v['key'],true);				
				$v['endtime']=date('Y-m-d H:i:s',$v['endtime']);
			}
		}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：兑换时间，以兑换码的有效期为准！','edit'=>false,'list'=>array(			
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'string'),
			array('name'=>'key','title'=>'兑换码','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		),
		'search_one'=>array('name'=>'key','title'=>'兑换码'),
		
	);
	$return['status']=array('0'=>'已兑换','1'=>'未兑换');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	$g=pdo_get('jy_exchange_group',array('id'=>$_SESSION['exid'],'uniacid'=>$_SESSION['uniacid']));
	if($g['endtime']<time()){
		exi('活动已结束','error');
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post2',
		'desc'=>'设置',
		'tabs'=>array(
		//	array('op'=>'list','title'=>'返回兑换列表'),
		),
		'edit'=>array(
				array('name'=>'time','title'=>'有效期（单位：天）','type'=>'string'),
				array('name'=>'total','title'=>'数量','type'=>'string'),
		)
		);
		$return['item']['id']=$_SESSION['exid'];
	
		$return['htmls']=$htmls;
		exi($return);
	
}
if($_RQ['op']=='post2'){	
	$post=get_postdata();	
	$days=intval($post['time'])?:3;
	for($x=0; $x<$post['total']; $x++){
		$key=rand(100000,999999);
		$data=array('groupid'=>$post['id'],'endtime'=>time()+$days*24*3600,'key'=>$key,'uniacid'=>$_SESSION['uniacid']);
		pdo_insert('jy_exchange_code',$data);
	}
	pdo_update('jy_exchange_group',array('total +='=>$post['total']),array('id'=>$post['id']));
	$code=pdo_getall('jy_exchange_code',array('groupid'=>$post['id']),array('key'));
	$string="把下面的兑换码，提供给印刷商，批量打印出二维码。\r\n";
	foreach($code as $item){
		$string.=custom_url('jy_exchange/index//code='.$item['key'],true)."\r\n";
	}
	$code_file = ROOT_AT.random(10).".zip";
	$zip = new ZipArchive();
	$zip->open($code_file,ZipArchive::CREATE);
	$zip->addFromString('code.txt',$string);
	$zip->close();
	$zip=cfc('files')->source_save($code_file,'zip','zip');
	pdo_update('jy_exchange_group',array('code_file'=>$zip),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
	$type=pdo_getcolumn('jy_exchange_group',array('id'=>$post['id']),'type');
	
	exi('操作成功','','/code/list/type='.$type.'&id='.$post['id']);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_exchange_code',array('id'=>$id));
		}
	}
	exi('删除成功！');
}
